package com.yxinsur.imc.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcContractFile;
import com.yxinsur.imc.business.form.ContractFileForm;
import com.yxinsur.imc.business.mapper.ImcCommMapper;
import com.yxinsur.imc.business.mapper.ImcContractFileMapper;
import com.yxinsur.imc.business.service.IImcContractFileService;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ImcContractFileServiceImpl  extends ServiceImpl<ImcContractFileMapper, ImcContractFile> implements IImcContractFileService {

    @Autowired
    ImcContractFileMapper imcContractFileMapper;
    /**
     * 更新协议  文件关系
     * @param contractId
     * @param contractFiles
     */
    @Override
    public void saveNewFile(Long contractId, List<ContractFileForm> contractFiles) {
        List<ImcContractFile> contractFileOlds = imcContractFileMapper.selectByContractId(contractId);
        for (ImcContractFile contractFile : contractFileOlds) {
            contractFile.setIsDeleted(contractFile.getId().toString());
            imcContractFileMapper.updateById(contractFile);
        }

        for (ContractFileForm contractFile : contractFiles) {
            ImcContractFile imcContractFile = new ImcContractFile();
            imcContractFile.setContractId(contractId);
            imcContractFile.setFileUrl(contractFile.getFileUrl());
            imcContractFile.setFileName(contractFile.getFileName());
            imcContractFile.setTenantId(SecurityUtils.getTenantId());
            imcContractFile.setCreateId(SecurityUtils.getUsername());
            imcContractFile.setCreateTime(DateUtils.getNowDate());
            imcContractFile.setUpdateId(SecurityUtils.getUsername());
            imcContractFile.setUpdateTime(DateUtils.getNowDate());
            imcContractFileMapper.insertImcContractFile(imcContractFile);
        }

    }
}
